﻿// Jinx AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Formality.FormalSystem
{
    //using EdgeFunction = Func<Variable, IEnumerable<Variable>>;

    //public static class TypeChecker
    //{
    //    public static void TypeCheck(this Sentence Sentence)
    //    {
    //        FunctionArity(Sentence);

    //        var Graph = BuildFunctionGraph(Sentence);

    //        NotRecursive(Graph);
    //    }

    //    private static FuncGraph BuildFunctionGraph(Sentence Sentence)
    //    {
    //        var Map = new Dictionary<Variable, Set<Variable>>();

    //        foreach (var f in Sentence.VisitAll<Function>())
    //        {
    //            Set<Variable> ArgSet;

    //            if (!Map.TryGetValue(f.Name, out ArgSet))
    //            {
    //                ArgSet = new Set<Variable>();
    //                Map[f.Name] = ArgSet;
    //            }

    //            foreach (var a in f.Arguments)
    //            {
    //                if (!Map.ContainsKey(a))
    //                    Map[a] = new Set<Variable>();
    //                ArgSet.Add(a);
    //            }
    //        }

    //        return new FuncGraph(Map.Keys, f => Map[f]);
    //    }

    //    private static void NotRecursive(FuncGraph G)
    //    {
    //        if (Graph.Cyclic(G.StartingGraph, G.EdgeFunction))
    //            throw new TypeViolation("Function recursively defined");
    //    }

    //    private static void FunctionArity(Sentence Sentence)
    //    {
    //        var Memo = new Dictionary<Variable, int>();

    //        foreach (var f in Sentence.VisitAll<Function>())
    //        {
    //            var fArgs = f.Arguments.Count();

    //            int Args;

    //            if (Memo.TryGetValue(f.Name, out Args))
    //            {
    //                if (Args != fArgs)
    //                    throw new TypeViolation(string.Format("Function {0} does not have a consistent number of arguments", f.Name));
    //            }
    //            else
    //            {
    //                Memo[f.Name] = fArgs;
    //            }
    //        }
    //    }

    //    private class FuncGraph : Tuple<IEnumerable<Variable>, EdgeFunction>
    //    {
    //        public FuncGraph(IEnumerable<Variable> StartingGraph, EdgeFunction EdgeFunction)
    //            : base(StartingGraph, EdgeFunction) { }

    //        public IEnumerable<Variable> StartingGraph { get { return Item1; } }
    //        public EdgeFunction EdgeFunction { get { return Item2; } }

    //        public override string ToString()
    //        {
    //            return (from v in StartingGraph
    //                    from a in EdgeFunction(v)
    //                    select string.Format("{0} -> {1}", v.Detailed, a.Detailed))
    //                    .Join(", ");
    //        }
    //    }
    //}
}
